﻿using System;
using System.IO;
using System.Text;
using System.Threading;

namespace COUP.Core
{
    public class Log
    {
        private static StreamWriter _writer;
        private static FileStream _file;

        public Log(string Name = "")
        {
            if (!Directory.Exists(Environment.CurrentDirectory + @"\Logs"))
                Directory.CreateDirectory(Environment.CurrentDirectory + @"\Logs");

            if (Name == "")
                Name += Environment.CurrentDirectory + @"\Logs\" + DateTime.Now.ToString("HH-mm-ss--dd-MM-yy");

            _file = new FileStream(Name + ".txt", FileMode.OpenOrCreate);
            _writer = new StreamWriter(_file);
            _writer.AutoFlush = true;
        }

        public void WriteLine(string _line)
        {
            while (!Monitor.TryEnter(_writer, 50)) { }

            _writer.WriteLine(_line);

            Monitor.Exit(_writer);
        }

        public void Close()
        {
            _writer.Close();
        }
    }
}
